<template>
  <div class="app-viewer">
    <div class="viewer-header">
      <el-row>
        <el-col :span="12">
          <el-row :gutter="10" class="mb20">
            <el-col :span="1.5">
              <el-button type="primary" @click="handleAdd"> 新增 </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button plain :disabled="multiple" @click="handleDelete">
                删除
              </el-button>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12" style="text-align: right">
          <transition name="fade">
            <span v-show="!isShowQuery" style="margin-right: 10px">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入矿山名称"
                style="width: 200px; margin-right: 10px"
              ></el-input>
              <el-button type="primary" @click="handleQuery"> 查询 </el-button>
              <el-button type="primary" @click="resetQuery"> 重置 </el-button>
            </span>
          </transition>
          <!-- <el-button type="primary" @click="switchQuery">
            {{ isShowQuery ? "普通检索" : "高级检索" }}
          </el-button> -->
        </el-col>
      </el-row>
      <!-- <el-collapse-transition>
        <div class="query" v-show="isShowQuery">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="100px"
            @submit.native.prevent
          >
            <el-form-item prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入所属火区地址"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button type="primary" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-collapse-transition> -->
    </div>
    <el-tabs v-model="activeName">
      <el-tab-pane label="井工煤矿" name="jinggong"></el-tab-pane>
      <el-tab-pane label="露天煤矿" name="loutian"></el-tab-pane>
      <el-tab-pane label="尾矿库" name="weikuang"></el-tab-pane>
    </el-tabs>
    <div class="viewer-body">
      <el-table
        v-loading="loading"
        :data="infoList"
        border
        :header-cell-style="{
          background: '#Fff',
          fontFamily: 'AlibabaPuHuiTi-2-55-Regular',
        }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="矿山名称" align="center" prop="name" />
        <!-- <el-table-column prop="lonAndLat" align="center" label="经纬度">
        </el-table-column> -->
        <el-table-column label="矿山地址" align="center" prop="address" />
        <el-table-column
          label="操作"
          align="center"
          width="250"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <div class="hanlderArea">
              <div @click="handleUpdate(scope.row)" class="checkDetail">
                编辑
              </div>
              <div @click="handleDetail(scope.row)" class="checkDetail">
                查看
              </div>
              <div @click="handleExport(scope.row)" class="checkDetail">
                导出
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        layout="total, prev,pager,next"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!--对话框 -->
    <!-- <el-dialog
      :title="title"
      :visible.sync="open"
      class="open-container"
      @closed="closeDialog"
      :modal-append-to-body="false"
    >
      <el-form
        :inline="true"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="140px"
        @submit.native.prevent
      >
        <el-form-item label="煤矿名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入煤矿名称"
            style="width: 300px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog> -->
    <CoalJGDialoag
      ref="jgDialog"
      :serviceRefresh="getList"
      :dialogTitle="dialogTitle"
    />
    <CoalLTDialoag
      ref="ltDialog"
      :serviceRefresh="getList"
      :dialogTitle="dialogTitle"
    />
    <CoalWKKialoag
      ref="wkkDialog"
      :serviceRefresh="getList"
      :dialogTitle="dialogTitle"
    />
  </div>
</template>

<script>
import {
  jingGongList,
  weiKuangList,
  louTianList,
  updateWeiKuang,
  updateLouTian,
  updateJingGong,
  addWeiKuang,
  addLouTian,
  addJingGong,
  delWeiKuang,
  delJingGong,
  delLouTian,
  checkWeiKuangDetails,
  checkJingGongDetails,
  checkLouTianDetails,
} from "@/api/data/houtai";
import axios from "axios";
import CoalJGDialoag from "./CreateOrUpdateJingGongDialog.vue";
import CoalLTDialoag from "./CreateOrUpdateLTDialog.vue";
import CoalWKKialoag from "./CreateOrUpdateWKKDialog.vue";
import { getToken } from "@/utils/auth";

export default {
  name: "Coal",
  components: { CoalJGDialoag, CoalLTDialoag, CoalWKKialoag },
  computed: {
    dialogTitle: function () {
      // if (this.dialogStatus === "create") {
      //   return "添加火区";
      // } else if (this.dialogStatus === "detail") {
      //   return "火区详情";
      // } else if (this.dialogStatus === "edit") {
      //   return "修改火区";
      // }
      return "";
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史数据表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 非多个禁用
      multiple: true,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
      },

      isShowQuery: false,
      activeName: "jinggong",
      dialogStatus: "",
    };
  },
  mounted() {
    this.getList();
  },
  watch: {
    activeName: {
      handler() {
        this.getList();
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      let list = jingGongList;
      switch (this.activeName) {
        case "jinggong":
          list = jingGongList;
          break;
        case "loutian":
          list = louTianList;
          break;
        case "weikuang":
          list = weiKuangList;
          break;
      }
      list({
        ...this.queryParams,
      }).then((response) => {
        this.infoList = response.data.rows.map((item) => {
          const ojb = { ...item };
          ojb.qaType = "无";
          if (item.longitude && item.latitude) {
            ojb.lonAndLat =
              item.longitude.toFixed(3) + "," + item.latitude.toFixed(3);
          }
          if (item.ifAlarm == 2) {
            ojb.qaType = "报警";
          }
          if (item.ifFault == 2) {
            ojb.qaType += ", 故障";
          }

          return ojb;
        });
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: "",
      };
      this.dateRange = [];
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      let curDialog = "jgDialog";
      switch (this.activeName) {
        case "jinggong":
          this.title = "新增井工煤矿";
          curDialog = "jgDialog";
          break;
        case "loutian":
          this.title = "新增露天煤矿";
          curDialog = "ltDialog";
          break;
        case "weikuang":
          curDialog = "wkkDialog";
          this.title = "新增尾矿库";
          break;
      }
      this.dialogStatus = "create";
      this.$refs[curDialog].openDialog("create");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.dialogStatus = "edit";
      let detail = checkJingGongDetails;
      let curDialog = "jgDialog";
      switch (this.activeName) {
        case "jinggong":
          this.title = "修改井工煤矿";
          curDialog = "jgDialog";
          detail = checkJingGongDetails;
          break;
        case "loutian":
          this.title = "修改露天煤矿";
          curDialog = "ltDialog";
          detail = checkLouTianDetails;
          break;
        case "weikuang":
          curDialog = "wkkDialog";
          this.title = "修改尾矿库";
          detail = checkWeiKuangDetails;
          break;
      }
      detail(row.id).then((response) => {
        this.form = response.data;
        this.form.id = row.id;
        if (this.form.latitude && this.form.longitude)
          this.form.latitudeAndLongitude =
            this.form.longitude + " ；" + this.form.latitude;

        this.$refs[curDialog].openDialog("edit", this.form);
      });
    },
    /** 传感器历史数据 */
    handleDetail(row) {
      this.dialogStatus = "detail";
      let detail = checkJingGongDetails;
      let curDialog = "jgDialog";
      switch (this.activeName) {
        case "jinggong":
          this.title = "井工煤矿详情";
          detail = checkJingGongDetails;
          break;
        case "loutian":
          this.title = "露天煤矿详情";
          curDialog = "ltDialog";
          detail = checkLouTianDetails;
          break;
        case "weikuang":
          this.title = "尾矿库详情";
          curDialog = "wkkDialog";
          detail = checkWeiKuangDetails;
          break;
      }
      detail(row.id).then((response) => {
        this.form = response.data;
        this.form.id = row.id;
        this.$refs[curDialog].openDialog("detail", this.form);
      });
    },
    switchQuery() {
      this.isShowQuery = !this.isShowQuery;
      this.queryParams.name = "";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const that = this;
      this.$modal
        .confirm("删除为重要数据，请确认是否删除？")
        .then(function () {})
        .then(() => {
          that.$modal
            .confirm("删除为重要数据，请再次确认是否删除？")
            .then(function () {
              let deletePromise = delJingGong;
              switch (that.activeName) {
                case "jinggong":
                  deletePromise = delJingGong;
                  break;
                case "loutian":
                  deletePromise = delLouTian;
                  break;
                case "weikuang":
                  deletePromise = delWeiKuang;
                  break;
              }

              return deletePromise(ids);
            })
            .then(() => {
              this.resetQuery();
              this.$modal.msgSuccess("删除成功");
            })
            .catch(() => {});
        })
        .catch(() => {});
    },

    /** 提交按钮 */
    submitForm() {
      // this.$refs["form"].validate((valid) => {
      //   if (valid) {

      //   }
      // });
      let add = addJingGong;
      let update = updateJingGong;
      switch (this.activeName) {
        case "jinggong":
          update = updateJingGong;
          add = addJingGong;
          break;
        case "loutian":
          update = updateLouTian;
          add = addLouTian;
          break;
        case "weikuang":
          update = updateWeiKuang;
          add = addWeiKuang;
          break;
      }
      if (this.form.id != null) {
        update(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        add(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 导出按钮操作 */
    handleExport(row) {
      const baseURL = process.env.VUE_APP_BASE_API;
      let url = baseURL + "/jinggongmine/basic/exportPdf/";
      switch (this.activeName) {
        case "jinggong":
          url = baseURL + "/jinggongmine/basic/exportPdf/";
          break;
        case "loutian":
          url = baseURL + "/opencutmine/basic/exportPdf/";
          break;
        case "weikuang":
          url = baseURL + "/pond/basic/exportPdf/";
          break;
      }
      axios
        .get(url + row.id, {
          responseType: "blob",
          headers: { Authorization: "Bearer " + getToken() },
        })
        .then(({ data }) => {
          const link = document.createElement("a");
          const blob = new Blob([data]);
          link.href = URL.createObjectURL(blob);
          link.download = `${row.name}.pdf`;
          link.click();
          URL.revokeObjectURL(link.href);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
#myChart {
  width: 960px;
  height: 420px;
}
.query {
  border-top: 1px solid #dfe6ec;
  border-bottom: 1px solid #dfe6ec;
  text-align: right;
}
.query >>> .el-form--inline .el-form-item {
  margin-right: 0;
  margin-left: 10px;
  margin-bottom: 14px;
  margin-top: 14px;
}
.app-container >>> .el-button--text:hover {
  border-color: #0000;
  background: #0000;
}
::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: 1px solid #4b5057 !important;
}
::v-deep .el-table::before,
.el-table--group::after,
.el-table--border::after {
  border: solid 1px #4b5057 !important;
}
::v-deep .el-table--border:after,
.el-table--group:after,
.el-table:before {
  background-color: #4b5057 !important;
}

::v-deep .el-table--border,
.el-table--group {
  border-color: #4b5057 !important;
}

::v-deep.el-table td,
.el-table th.is-leaf {
  border-bottom: 1px solid #4b5057 !important;
}

::v-deep .el-table--border th,
.el-table--border th.gutter:last-of-type {
  border-bottom: 1px solid #4b5057 !important;
}

::v-deep.el-table--border td,
.el-table--border th {
  border-right: 1px solid #4b5057 !important;
}
::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: 1px solid rgba(255, 255, 255, 0.17);
  border-right: 1px solid #4b5057 !important;
}

::v-deep .el-table tbody tr:hover > td {
  background-color: #1d2125 !important;
  color: #ffffff;
}

.hanlderArea {
  display: flex;
  justify-content: space-around;
  flex-direction: row;
  .checkDetail {
    font-family: "AlibabaPuHuiTi-2-55-Regular";
    font-size: 15px;
    font-weight: normal;
    font-stretch: normal;
    letter-spacing: 1px;
    color: #005aff;
    cursor: pointer;
  }
}
::v-deep .el-tabs__item {
  color: #999999;
  font-family: "AlibabaPuHuiTi-2-55-Regular";
  font-size: 14px;
  .is-active {
    font-size: 16px;
  }
}
// color: "#005aff" !important;

::v-deep .el-tabs__nav-wrap::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 2px;

  background-color: rgba(153, 153, 153, 0.3);
}
</style>
